from django.db import transaction
from rest_framework import serializers
from spitslot.models import Tsukkomi_Details, Comments, CommentLike, SpitLike
from users.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username')

# 首页序列化
class TsukkomiListSeriazlier(serializers.ModelSerializer):
    user_id = UserSerializer(read_only=True)

    class Meta:
        model = Tsukkomi_Details
        fields = '__all__'



# 吐槽评论
class news_content_serializer(serializers.ModelSerializer):
    class Meta:
        model=Comments
        fields=('user_id','content','parent_id','like_content')



class SpitLikeSerializer(serializers.ModelSerializer):
    class Meta:
        model = SpitLike
        fields = ('spit_id',)




class SpitLikeActionSerializer(serializers.ModelSerializer):
    """点赞取消点赞序列化器"""
    action = serializers.CharField(label='点赞', write_only=True)

    class Meta:
        model = CommentLike
        fields = ('comment_id', 'action')

    def validate_action(self, value):
        """检验用户是否同意协议"""
        if value not in ['add', 'cancel']:
            raise serializers.ValidationError('数据传输错误')
        return value




